package com.zy.ai.service;

import cn.hutool.core.lang.UUID;
import com.zy.config.QdrantClient;
import dev.langchain4j.data.embedding.Embedding;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.model.output.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class AiVectorService {

    @Autowired
    private QdrantClient qdrantClient;

    @Autowired
    private EmbeddingModel embeddingModel; // 注入 embedding 模型


    public List<Double> getEmbedding(String question) {
        // 生成 embedding
        Response<Embedding> response = embeddingModel.embed(question);
        Embedding embedding = response.content();

        // 转换成 List<Double>
        float[] vector = embedding.vector();
        List<Double> result = new ArrayList<>(vector.length);
        for (float v : vector) {
            result.add((double) v);
        }
        return result;
    }

    public void saveQuestionEmbedding(String collection, String question, String answer, String questionId ) {
        List<Double> embedding = getEmbedding(question);
        Map<String,Object> payload = Map.of("question", question, "answer", answer);
        qdrantClient.upsertVector(collection, questionId, embedding, payload);
    }

    public List<Map<String,Object>> searchSimilar(String collection, List<Double> embedding, int topK) {
        return qdrantClient.searchSimilar(collection, embedding, topK);
    }
}
